package com.byzx.wine.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzx.wine.dao.SyroleauthorityMapper;
import com.byzx.wine.po.Syauthority;
import com.byzx.wine.po.Syroleauthority;
import com.byzx.wine.service.ISyroleauthorityService;
@Service
public class SyroleauthorityService implements ISyroleauthorityService {
	@Autowired
	private SyroleauthorityMapper syroleauthority;
	@Override
	public int deleteByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return syroleauthority.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Syroleauthority record) {
		// TODO Auto-generated method stub
		return syroleauthority.insert(record);
	}

	@Override
	public int deleteSelective(Syroleauthority record) {
		// TODO Auto-generated method stub
		return syroleauthority.deleteSelective(record);
	}

	@Override
	public List<Map<String, Object>> Syroleauthority (Integer id) {
		List<Map<String, Object>> llist = new ArrayList<Map<String, Object>>();
		List<Syroleauthority> lists = syroleauthority.Syroleauthority(id);
		List<Syauthority> list = syroleauthority.findSyauthority();
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", list.get(i).getId());
			map.put("pid", list.get(i).getPid());
			map.put("name", list.get(i).getName());
			map.put("src", list.get(i).getSrc());
			map.put("open", false);
			for (int j = 0; j < lists.size(); j++) {
				if (lists.get(j).getAuthid().equals(list.get(i).getId())) {
					map.put("checked", true);
					break;
				} else {
					map.put("checked", false);
				}
			}
			llist.add(map);
		}
		
		return llist;
	}

	@Override
	//进行授权处理
	public Integer dealAuthority(Integer roleid,String ids){
		Integer back = 0;
		Syroleauthority re = new Syroleauthority();
		re.setRoleid(roleid);
		syroleauthority.deleteSelective(re);
		String [] str = ids.split(",");
		for(int i = 0;i<str.length;i++){
			Syroleauthority record = new Syroleauthority();
			record.setRoleid(roleid);
			record.setAuthid(Integer.parseInt(str[i]));
			back += syroleauthority.insertSelective(record);
		}
		return back;
	}

}
